package com.caits.lbs.framework.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.caits.lbs.framework.Constants;
import com.caits.lbs.framework.log.CommonLogFactory;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * <p>SystemDefaultInterceptor</p>
 * <p>系统默认拦截器</p>
 *
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version		0.0.0
 * <table style="border:1px solid gray;">
 * <tr>
 * <th width="100px">版本号</th><th width="100px">动作</th><th width="100px">修改人</th><th width="100px">修改时间</th>
 * </tr>
 * <!-- 以 Table 方式书写修改历史 -->
 * <tr>
 * <td>0.0.0</td><td>创建类</td><td>zhouhuabin</td><td>2014年9月25日 下午1:25:27</td>
 * </tr>
 * <tr>
 * <td>XXX</td><td>XXX</td><td>XXX</td><td>XXX</td>
 * </tr>
 * </table>
*/
public class SystemDefaultInterceptor extends AbstractInterceptor
{
	/** 系列号 */
	private static final long serialVersionUID = 1L;

	/** 日志记录器 */
	private Logger log = CommonLogFactory.getLog();

	@Override
	public String intercept(ActionInvocation invocation) throws Exception
	{
		boolean isLogined = before(invocation);
		// isLogined = true;
		if (isLogined)
		{
			String result = invocation.invoke();
			behind(invocation);
			return result;
		}
		else
		{
			log.error("用户还没有登陆!");
			HttpServletResponse response = (HttpServletResponse) invocation.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
			HttpServletRequest request = (HttpServletRequest) invocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
			// 清空session
			request.getSession().invalidate();
			if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest"))
			{
				response.setHeader("isLogined", "noLogin");
			}
			else
			{
				log.warn("严重错误，用户没有登陆");
				response.sendRedirect(request.getContextPath() + "/index.html");
			}
			return Action.SUCCESS;
		}

	}

	/**
	 * 处理前调用 
	 * @param invocation
	 * @return
	 * @throws Exception
	 */
	public boolean before(ActionInvocation invocation) throws Exception
	{
		log.debug("action调用开始");
		boolean isLogined = false;
		// /boolean isLogined = true;
		Map<String, Object> session = invocation.getInvocationContext().getSession();
		Object action = invocation.getAction();
		if (action instanceof BaseAction)
		{
			BaseAction baseAction = (BaseAction) action;
			// 验证是否登陆时保存过用户对象
			if (session.get(Constants.WEB_SESSION) != null)
			{
				baseAction.setSession(session);
				return true;
			}
		}
		return isLogined;
	}

	/**
	 * 处理后调用 
	 * @param invocation
	 * @throws Exception
	 */
	public void behind(ActionInvocation invocation) throws Exception
	{
		Object action = invocation.getAction();
		log.debug("拦截器中action调用完毕"+action.getClass().getCanonicalName());
	}
}
